package com.xywl.escort.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.SweepGradient;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

import com.xywl.escort.R;

public class CircleProgress extends View {

    //定义颜色
    private int ring_color, ring_progress_color;

    private float ringSize;

    //动画
    ValueAnimator animator;

    //增长动画中的临时进度
    private float tempProgress = 0;
    //当前进度
    private int curProgress;
    //最大进度
    private int maxProgress;

    //定义画笔
    private Paint mPaint;
    //圆弧
    private Paint ringPaint;
    //进度圆弧
    private Paint ringProgressPaint;

    public CircleProgress(Context context) {
        this(context, null);
    }

    public CircleProgress(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleProgress(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获得atts.xml定义的属性值，存储在TypedArray中
        TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CircleProgress, defStyleAttr, 0);
        int n = ta.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = ta.getIndex(i);
            switch (attr) {
                /*
                 * 圆环颜色
                 */
                case R.styleable.CircleProgress_ringColors: {
                    ring_color = ta.getColor(attr, Color.BLACK);
                }
                break;
                /*
                 * 圆环宽度
                 */
                case R.styleable.CircleProgress_ringSize: {
                    ringSize = ta.getDimension(R.styleable.CircleProgress_ringSize, 13);
                }
                break;
                /*
                 * 圆环进度颜色
                 */
                case R.styleable.CircleProgress_ringprogressColor: {
                    ring_progress_color = ta.getColor(attr, Color.WHITE);
                }
                break;
                default:
                    break;
            }
        }
        ta.recycle();
        init();
    }

    private void init() {
        //画笔
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        //圆弧
        ringPaint = new Paint();
        ringPaint.setAntiAlias(true);
        //进度圆弧
        ringProgressPaint = new Paint();
        ringProgressPaint.setAntiAlias(true);
    }

    /**
     * 重写onMeasure()方法，支持wrap_content属性
     *
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width;
        int height;
        //宽度的测量模式
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        //宽度的测量值
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        //高度的测量模式
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        //高度的测量值
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        //如果布局里面设置的是固定值,这里取布局里面的固定值;如果设置的是match_parent,则取父布局的大小
        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else {
            //如果布局里面没有设置固定值,这里取布局的宽度的1/2
            width = widthSize / 2;
        }
        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            //如果布局里面没有设置固定值,这里取布局的高度的3/4
            height = heightSize * 3 / 4;
        }
        setMeasuredDimension(width, height);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 画最外层的大圆环
        //获取圆心的x坐标
        int centre = getWidth() / 2;
        //圆环的半径
        int radius = (int) (centre - 2 * ringSize);
        //设置圆环的颜色
        mPaint.setColor(ring_color);
        //设置空心
        mPaint.setStyle(Paint.Style.STROKE);
        //设置圆环的宽度
        mPaint.setStrokeWidth(ringSize);
        //消除锯齿
        mPaint.setAntiAlias(true);
        //画出圆环
        canvas.drawCircle(centre, centre, radius, mPaint);

        //画圆弧
        ringProgressPaint.setStyle(Paint.Style.STROKE);
        ringProgressPaint.setStrokeWidth(ringSize);

        ringProgressPaint.setColor(ring_progress_color);
        //用于定义的圆弧的形状和大小的界限
        RectF oval = new RectF(centre - radius, centre - radius, centre + radius, centre + radius);

        //创建一个渲染器
        SweepGradient mSweepGradient = new SweepGradient(canvas.getWidth() / 2
                , canvas.getHeight() / 2, new int[]{Color.rgb(130, 213, 131), Color.rgb(150, 251, 196), Color.rgb(130, 213, 131)}, null);
        Matrix matrix = new Matrix();
        matrix.setRotate(-90f, canvas.getWidth() / 2, canvas.getHeight() / 2);
        mSweepGradient.setLocalMatrix(matrix);
        // ringProgressPaint.setShader(mSweepGradient);
        canvas.drawArc(oval, -90, 360 * tempProgress / maxProgress, false, ringProgressPaint);
    }

    public synchronized void setMax(int maxProgress) {
        if (maxProgress < 0) {
            throw new IllegalArgumentException("maxProgress not less than 0");
        }
        this.maxProgress = maxProgress;
    }

    /**
     * 开启动画
     *
     * @param curProgress
     */
    public void setProgressWithAnimation(int curProgress) {
        this.curProgress = curProgress;
        animator = ValueAnimator.ofInt(0, curProgress);
        animator.setDuration(3000);
        animator.setInterpolator(new LinearInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                tempProgress = (int) animation.getAnimatedValue();
                invalidate();
            }
        });
        animator.start();
    }

    public void setProgress(int curProgress) {
        this.curProgress = curProgress;
        tempProgress = curProgress;
        invalidate();
    }

    /**
     * 完成测量
     *
     * @param curProgress
     */
    public void completeMeasure(int curProgress) {
        animator.cancel();
        this.curProgress = curProgress;
        tempProgress = curProgress;
        invalidate();
    }

    /**
     * 停止Progress
     */
    public void stopProgress() {
        animator.cancel();
        invalidate();
    }

}